package com.bwie.rent.service.impl;

import com.bwie.common.utils.R;
import com.bwie.rent.conf.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.rent.dao.ComplainDao;
import com.bwie.rent.entity.ComplainEntity;
import com.bwie.rent.service.ComplainService;


@Service("complainService")
public class ComplainServiceImpl extends ServiceImpl<ComplainDao, ComplainEntity> implements ComplainService {

    @Autowired
    ComplainDao complainDao;

    @Autowired
    JwtUtil jwtUtil;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ComplainEntity> page = this.page(
                new Query<ComplainEntity>().getPage(params),
                new QueryWrapper<ComplainEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R compliain(String user, ComplainEntity complainEntity) {
        complainEntity.setComplainTime(new Date());
        if (!"-1".equals(user)){
            Claims claims = jwtUtil.parseJWT(user);
            if (claims!=null){
                String id = claims.getId();
                complainEntity.setComplainUserId(id);
            }
            complainDao.insert(complainEntity);
            return R.ok();
        }else {
            String s = UUID.randomUUID().toString().replaceAll("-", "");
            complainEntity.setComplainUserId(s);
            complainDao.insert(complainEntity);
            return R.ok();
        }
    }

}